package com.twetec.cms.dao.mapper.system;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.twetec.cms.model.po.system.PermissionPO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 权限表 Mapper 接口
 * </p>
 *
 * @author zhxl
 * @since 2020-10-12
 */
public interface PermissionMapper extends BaseMapper<PermissionPO> {

    @Select("SELECT SP.* FROM SYSTEM_PERMISSION SP " +
            "    INNER JOIN SYSTEM_ROLE_PERMISSION SRP " +
            "    ON SP.ID = SRP.PERMISSION_ID " +
            "    INNER JOIN SYSTEM_ROLE_USER SOU " +
            "    ON SRP.ROLE_ID = SOU.ROLE_ID " +
            "    WHERE SOU.USER_ID = #{userId}")
    List<PermissionPO> getPermissionListByUserId(@Param("userId") Long userId);

    @Update("UPDATE system_permission A SET A.PID = (" +
            "SELECT B.ID FROM system_permission B  WHERE A.UNION_PID = B.UNION_PERMISSION_ID AND ROWNUM < 2) " +
            "WHERE EXISTS (SELECT 1 FROM system_permission B WHERE A.UNION_PID = B.UNION_PERMISSION_ID )")
    boolean batchUpdatePid();
}
